//
//  LYSelectCompanyHeader.m
//  SelectCity
//
//  Created by liuyan on 2019/3/22.
//  Copyright © 2019年 liuyan. All rights reserved.
//

#import "LYSelectCompanyHeader.h"
#import "LYSelectCompanyCollectionCell.h"

@interface  LYSelectCompanyHeader()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UITextFieldDelegate>

@property (nonatomic, strong) UICollectionView *selectCollect;
@property (nonatomic, strong) UILabel *countLabel;
@property (nonatomic, strong) NSArray *dataArray;

@end

@implementation LYSelectCompanyHeader

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubview];
    }
    return self;
}

- (void)createSubview {
    _countLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH-200*Scale, 10*Scale, 185*Scale, 30*Scale)];
    _countLabel.textAlignment = NSTextAlignmentRight;
    _countLabel.font = Font(Font_13);
    _countLabel.textColor = [UIColor orangeColor];
    [self addSubview:_countLabel];
    
    UILabel *titleLabel =  [[UILabel alloc] initWithFrame:CGRectMake(15*Scale, 10*Scale, 200*Scale, 30*Scale)];
    titleLabel.font = FontBold(Font_18);
    titleLabel.textColor = THEMECOLOR_OF_TEXT_333333;
    titleLabel.text = @"选择公司行业";
    [self addSubview:titleLabel];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 15*Scale;//最小行间距
    layout.minimumInteritemSpacing = 15*Scale; //最小列间距
    CGRect frame = CGRectMake(15*Scale, titleLabel.bottom+10*Scale, WIDTH-30*Scale, 0);
    _selectCollect = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];
    _selectCollect.backgroundColor = [UIColor whiteColor];
    _selectCollect.showsHorizontalScrollIndicator = NO;
    _selectCollect.showsVerticalScrollIndicator = NO;
    _selectCollect.delegate = self;
    _selectCollect.dataSource = self;
    [_selectCollect registerClass:[LYSelectCompanyCollectionCell class] forCellWithReuseIdentifier:@"LYSelectCompanyCollectionCell"];
    [self addSubview:_selectCollect];
}
#pragma mark -- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LYSelectCompanyCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LYSelectCompanyCollectionCell" forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
//    __weak typeof(self) weakself = self;
    cell.block = ^(){
//        weakself.block(indexPath.row);
    };
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    __weak typeof(self) weakself = self;
    weakself.block(indexPath.row);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    PublicCategoryElementModel *model = self.dataArray[indexPath.row];
    return CGSizeMake(model.itemWidth + 20*Scale, 30*Scale);
}
- (void)setSelectArray:(NSArray *)selectArray {
    self.dataArray = selectArray;
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%li", selectArray.count]];
    NSAttributedString *strq = [[NSAttributedString alloc] initWithString:@"/最多选择3项" attributes:@{NSForegroundColorAttributeName:THEMECOLOR_OF_TEXT_9a9a9a, NSFontAttributeName:Font(Font_13)}];
    
    [str appendAttributedString:strq];
    _countLabel.attributedText = str;
    
    if (selectArray.count) {
        self.height = 100*Scale;
        self.selectCollect.height = 30*Scale;
    } else {
        self.height = 60*Scale;
        self.selectCollect.height = 0;
    }
    [self.selectCollect reloadData];
}

@end
